package com.lingyu.jsjx.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingyu.jsjx.domain.Evaluation;
import com.lingyu.jsjx.service.IEvaluationService;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 教师评价Controller
 *
 * @author ruoyi
 * @date 2025-09-05
 */
@RestController
@RequestMapping("/evaluation")
public class EvaluationController extends BaseController {
    @Autowired
    private IEvaluationService evaluationService;

    /**
     * 查询教师评价列表
     */
    @RequiresPermissions("system:evaluation:list")
    @GetMapping("/list")
    public TableDataInfo list(Evaluation evaluation) {
        startPage();
        List<Evaluation> list = evaluationService.selectEvaluationList(evaluation);
        return getDataTable(list);
    }

    /**
     * 导出教师评价列表
     */
    @RequiresPermissions("system:evaluation:export")
    @Log(title = "教师评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Evaluation evaluation) {
        List<Evaluation> list = evaluationService.selectEvaluationList(evaluation);
        ExcelUtil<Evaluation> util = new ExcelUtil<Evaluation>(Evaluation.class);
        util.exportExcel(response, list, "教师评价数据");
    }

    /**
     * 获取教师评价详细信息
     */
    @RequiresPermissions("system:evaluation:query")
    @GetMapping(value = "/{evaluationId}")
    public AjaxResult getInfo(@PathVariable("evaluationId") Long evaluationId) {
        return success(evaluationService.selectEvaluationByEvaluationId(evaluationId));
    }

    /**
     * 新增教师评价
     */
    @RequiresPermissions("system:evaluation:add")
    @Log(title = "教师评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Evaluation evaluation) {
        return toAjax(evaluationService.insertEvaluation(evaluation));
    }

    /**
     * 修改教师评价
     */
    @RequiresPermissions("system:evaluation:edit")
    @Log(title = "教师评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Evaluation evaluation) {
        return toAjax(evaluationService.updateEvaluation(evaluation));
    }

    /**
     * 删除教师评价
     */
    @RequiresPermissions("system:evaluation:remove")
    @Log(title = "教师评价", businessType = BusinessType.DELETE)
    @DeleteMapping("/{evaluationIds}")
    public AjaxResult remove(@PathVariable Long[] evaluationIds) {
        return toAjax(evaluationService.deleteEvaluationByEvaluationIds(evaluationIds));
    }

    @GetMapping(value = "/queryEvaluationScore")
    public AjaxResult queryEvaluationScore() {
        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<>();
        if (SecurityUtils.getUserId() != 1L) {
            queryWrapper.eq("create_by", SecurityUtils.getUserId());
        }
        List<Evaluation> evaluationList = evaluationService.list(queryWrapper);
        if (!evaluationList.isEmpty()) {
            BigDecimal totalScore = evaluationList.stream()
                    .map(Evaluation::getTotalScore)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal averageScore = totalScore.divide(new BigDecimal(evaluationList.size()), RoundingMode.HALF_UP);
            return success(averageScore);
        }else{
            return success(0);
        }
    }
}
